﻿Imports System.Web.UI.WebControls
Imports System.Web.UI
Imports DBase.scafolding.Conf
Imports DBase.MySql

Namespace DBase.scafolding

    Public Class CustomControData
        Inherits WebControl
        Implements INamingContainer

        Dim WithEvents LblField As New Label
        Dim WithEvents TxtField As New TextBox
        Dim WithEvents BtnOpen As New Button
        Dim WithEvents LblHour As New Label
        Dim WithEvents TxtHour As New TextBox
        Dim WithEvents LblMinute As New Label
        Dim WithEvents TxtMinute As New TextBox
        Dim WithEvents Calendar As New Calendar
        Dim WithEvents pnlCalendar As New Panel

        Public Sub New()
            Me.EnsureChildControls()
            Me.InitControl()
        End Sub

        Public Property Value() As String
            Get
                Dim DateResString As String
                Dim DateRes As Date
                DateResString = Me.TxtField.Text & " " & Me.TxtHour.Text & ":" & Me.TxtMinute.Text

                If Date.TryParse(DateResString, DateRes) Then
                    Return DateResString
                Else
                    Return Me.TxtField.Text
                End If
            End Get

            Set(value As String)
                Me.Calendar.SelectionMode = CalendarSelectionMode.Day
                Dim dd As Date
                'Me.Calendar.SelectedDate = DateSerial(dd.Year, dd.Month, dd.Day)  
                Me.Calendar.SelectedDate = New Date(dd.Year, dd.Month, dd.Day, dd.Hour, dd.Minute, dd.Second)

                Date.TryParse(value, dd)
                Me.TxtField.Text = dd.Date
                Me.TxtHour.Text = dd.Hour
                Me.TxtMinute.Text = dd.Minute
            End Set
        End Property

        Private Sub InitControl()

            LblField.Text = "Date:"
            TxtField.Enabled = True
            TxtField.Width = 120
            LblHour.Text = "HH:"
            TxtHour.Width = 40
            LblMinute.Text = "MM:"
            TxtMinute.Width = 40
            BtnOpen.Text = "Calendar"
            Calendar.CssClass = "Calendar_search"
            Me.Calendar.SelectedDayStyle.CssClass = "SelectedDayStyle"
            pnlCalendar.CssClass = "Panel_Calendar"
            Calendar.Visible = False
            Me.pnlCalendar.Controls.Add(Calendar)

        End Sub

        Private Sub CustomControlSearch_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            If Not Me.Page.IsPostBack Then
                Me.Calendar.Visible = False
            End If
        End Sub

        Protected Overrides Sub CreateChildControls()
            Controls.Clear()
            Controls.Add(LblField)
            Controls.Add(TxtField)
            Controls.Add(New LiteralControl("<br />"))
            Controls.Add(LblHour)
            Controls.Add(TxtHour)
            Controls.Add(New LiteralControl(" - "))
            Controls.Add(LblMinute)
            Controls.Add(TxtMinute)
            Controls.Add(New LiteralControl("<br />"))
            Controls.Add(BtnOpen)
            Controls.Add(New LiteralControl("<br />"))
            Controls.Add(pnlCalendar)
        End Sub

        Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
            For Each c As Object In Controls
                c.RenderControl(output)
            Next
        End Sub

        Protected Sub BtnOpen_Click(sender As Object, e As System.EventArgs) Handles BtnOpen.Click
            Me.Calendar.Visible = Not Me.Calendar.Visible

        End Sub

        Private Sub Calendar_SelectionChanged(sender As Object, e As System.EventArgs) Handles Calendar.SelectionChanged

            Me.TxtField.Text = Me.Calendar.SelectedDate

        End Sub
    End Class
End Namespace
